package com.zjitc.controller.Page;

import com.zjitc.module.Country;
import com.zjitc.module.Page;
import com.zjitc.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/21 15:08
 * @description:
 */
@SuppressWarnings("ALL")
@Controller
public class PageController {

    private static final int COUNT = 20;

    @Autowired
    private CountryService service;

    @RequestMapping("/pagefind/{pageid}")
    public ModelAndView findpage(@PathVariable("pageid") Integer pageid) {
        Page page = service.findbypage(pageid, COUNT);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/view/PageData.jsp");
        modelAndView.addObject("page", page);
        return modelAndView;
    }

    @RequestMapping("/pagesearchCountry")
    public ModelAndView searchCountry( String region) {
        System.out.println(region.length());
        ModelAndView mv = new ModelAndView();
        if(region.length() != 0) {
            Page page = service.pagesearch(region);
            mv.addObject("page", page);
            mv.setViewName("/view/PageData.jsp");
        }else{
            mv.setViewName("/pagefind/0.action");
        }
        return mv;
    }

    @RequestMapping("/pageadd")
    public View add(Country country) {
        service.add(country);
        View view = new InternalResourceView("/pagefind/0.action");
        return view;

    }

    @RequestMapping("/pagesinglefind/{code}")
    public ModelAndView change(@PathVariable("code") String code) {
        ModelAndView mv = new ModelAndView();
        Country country = service.findbycode(code);
        mv.addObject("country", country);
        mv.setViewName("/view/pagechange.jsp");
        return mv;
    }

    @RequestMapping("/pageupdate")
    public View modify(Country country) {
        service.update(country);
        View view = new InternalResourceView("/pagefind/0.action");
        return view;
    }

    @RequestMapping("/pagedelete/{code}")
    public View delete(@PathVariable("code") String code) {
        int d = service.delete(code);
        System.out.println("成功删除" + d + "条数据");
        View mv = new InternalResourceView("/pagefind/0.action");
        return mv;
    }
}
